package com.huluobo.a2210monthdemo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2210monthdemo.ui.theme._2210MonthDemoTheme
import com.huluobo.a2210monthdemo.views.MainPage
import com.huluobo.a2210monthdemo.views.WelcomePage
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_http.Const
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestPermissions(
            arrayOf(
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE
            ), 102
        )
        setContent {
            _2210MonthDemoTheme {
                // A surface container using the 'background' color from the theme
                var startPage = RoutePath.WELCOME
                val mmkv = MMKV.defaultMMKV()
                val navHostController = rememberNavController()

                //通过mmkv去读取是否已经进入过app,如果读取不到就是false
                val isIn = mmkv.decodeBool(Const.PARAM_IS_IN, false)
                if (isIn) {
                    //如果进入过app,就直接跳转到首页
                    startPage = RoutePath.MAIN
                } else {
                    //如果首次进入app,进入欢迎页并且倒计时5秒,再次进入首页
                    lifecycleScope.launch {
                        //延时5秒
                        async {
                            delay(5000)
                        }.await()
                        //跳转到首页
                        navHostController.navigate(RoutePath.MAIN)
                        //保存是否进入的值
                        mmkv.encode(Const.PARAM_IS_IN, true)
                    }
                }

                NavHost(navController = navHostController, startDestination = startPage, builder = {
                    composable(RoutePath.WELCOME) { WelcomePage() }
                    composable(RoutePath.MAIN) { MainPage() }
                })
            }
        }
    }
}